<?php
require_once 'util/httputil.php';
require_once 'util/CookieUtil.php';
/*ucenter*/
require_once __SITEPATH__.'/public/ucenter/config.inc.php';
require_once __SITEPATH__.'/public/uc_client/client.php';
require_once __SITEPATH__.'/public/ucenter/include/db_mysql.class.php';

class BaseController extends Zend_Controller_Action{
	protected $orm;
	protected $sysConfig;
	protected $admin_info;
	protected $request;
	protected $permissionConfig;
	
	public function init(){
		try {
			$this->orm = ORM::getInstance();
			$this->sysConfig = require_once __SITEPATH__ . '/application/configs/sysConfig.php';
			
			//校验用户登录
			$this->checkUserLogin();
			$this->request = $this->getRequest();
			
			//后台权限
			$this->permissionConfig = require_once __SITEPATH__ . '/application/configs/permissionConfig.php';
			$this->acl();
			
			//设置模版
			$this->view->setScriptPath(__SITEPATH__.'/template/admin/');
			
			//读取菜单
			$admin_menu = require_once __SITEPATH__ . '/application/configs/admin_menu_config.php';
			
			$this->view->assign(array(
				'system_title'			=> 'Wenda Admin',
				'admin_menu'			=> $admin_menu,
				'menu_value'			=> $this->request->getControllerName() . '/' . $this->request->getActionName(),
				'user_message'			=> uc_pm_list($this->admin_info['uid']),
				'permissionConfig'		=> $this->permissionConfig
			));
			
			$this->_init();
		}catch(Exception $e) {
			throw $e;
		}
	}
	
	//测试用户登录
	protected function checkUserLogin() {
		try {
			//检测登陆系统用户cookie
			$this->admin_info = explode("\t",uc_authcode(CookieUtil::cookieGet("wenda_system")));
			$this->admin_info['uid'] = $this->admin_info[0];
			$this->admin_info['username'] = $this->admin_info[1];
			$this->admin_info['group_id'] = $this->admin_info[2];
			$this->admin_info['permission'] = $this->admin_info[3];
			
			$this->view->admin_info = $this->admin_info;
			if (empty($this->admin_info['uid']) || empty($this->admin_info['username'])) {
				$this->_redirect(__PROJECTNAME__ . '/admin/login?url=' . $_SERVER['REQUEST_URI']);
			}
		}catch(Exception $e) {
			throw $e;
		}
	}
	
	//JS返回信息提示
	protected function alert_back($msg){
		echo('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />');
		echo('<script language="JavaScript">');
		echo("alert('$msg');");
		echo('history.back();');
		echo('</script>');
		exit;
	}
	
	//JS返回信息提示并跳转
	protected function alert_go($msg,$url){
		echo('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />');
		echo('<script language="JavaScript">');
		echo("alert('$msg');");
		echo("location.href='$url';");
		echo('</script>');
		exit;
	}
	
	//写入错误日志
	protected function toErrorLogs($e) {
		file_put_contents(__SITEPATH__ . '/logs/' . date('Y-m') . '_wenda.log',"#############################  " . date('Y-m-d H:i:s') . "  #############################\n" . $e . "\n\n\n",FILE_APPEND);
	}
	
	//校验用户访问权限
	protected function acl() {
		if($this->admin_info['permission'] != 'all') {
			$action = $this->request->getActionName();
			$controller = $this->request->getControllerName();
			
			if($this->permissionConfig['controllers'][$controller]['allow'] == 'none') {
				if($this->permissionConfig['methods'][$controller][$controller . '/' . $action]['allow'] == 'none') {
					if(!in_array($controller . '/' . $action,explode(',',$this->admin_info['permission']))) {
						$this->alert_back('您无权访问此页面！');
					}
				}
			}
		}
	}
	
	protected function _init(){
	}
}